package BitSetDemo;

/**
 * @Author Fizz Pu
 * @Date 2020/9/7 上午9:15
 * @Version 1.0
 * 失之毫厘，缪之千里！
 */
public class BitSet {
    public static void main(String[] args) {
        BitSet bitSet = new BitSet();
        bitSet.set(0);
        bitSet.set(0);
        bitSet.set(4);
        System.out.println(bitSet.get(0));
        System.out.println(bitSet.get(4));
        bitSet.del(0);
        System.out.println(bitSet.get(0));
        System.out.println(bitSet.get(1));
    }

    private int bits = 100;

    private byte[] arr;

    public BitSet(){
        arr = new byte[bits];
    }

    private int getArrIndex(int index){
          // index / 8
         return index >> 3;
    }

    private int getArrOffset(int index){
        // index % 8
        return index & (8-1);
    }

    public void set(int index){
        int pos = getArrOffset(index);
        int arrIndex = getArrIndex(index);
        arr[arrIndex] = (byte) (arr[arrIndex] | (1 << pos));
    }

    public void del(int index){
        int pos = getArrOffset(index);
        int arrIndex = getArrIndex(index);
        arr[arrIndex] = (byte) (~(1 << pos) & arr[arrIndex]);
    }

    public boolean get(int index){
        int pos = getArrOffset(index);
        int arrIndex = getArrIndex(index);
        return ((1 << pos) & arr[arrIndex]) != 0;
    }
}
